package utils;

import org.xutils.common.util.MD5;

import java.util.Arrays;
import java.util.HashMap;

import NDK.C_CommonUtils;
import common.Constant;

/**
 * Created by admin on 2017/2/13.
 */

public class SignatureUtil {
    private static final String TAG = "SignatureUtil";

    //获取时间戳
    public static String GetTimeStamp() {
        long timeStamp = System.currentTimeMillis();
        return String.valueOf(timeStamp);
    }

    //获取随机字符串
    public static String GetRandom(int count) {
        String random = "";
        String chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
        for (int i = 0; i < count; i++) {
            random += chars.charAt((int) (Math.random() * chars.length()));
        }
        return random;
    }

    public static String getSignature(HashMap<String, String> hashmap) {
        String finalString = "";
        if (hashmap.size() > 0) {
            Object[] key_arr = hashmap.keySet().toArray();
            Arrays.sort(key_arr);
            for (Object key : key_arr) {
                Object value = hashmap.get(key);
                finalString += key.toString() + "=" + value.toString();

                MyLogUtils.e(TAG,"------"+key.toString()+getCutLineLength(key.toString())+value.toString()+"\n");
            }
//            MyLogUtils.e(TAG,"------key"+getCutLineLength("key")+ Constant.KEY+"\n");
        }
        finalString += "key" + "=" + C_CommonUtils.getKey();
        String md5 = MD5.md5(finalString).toUpperCase();
        MyLogUtils.e(TAG,"------Signature"+getCutLineLength(md5)+md5+"\n");
        return md5;
    }

    private static String getCutLineLength(String value){
        String cutLine = "";
        int total = 50;
        int num = total - value.length();
        if(num>0) {
            for (int i = 0; i < num; i++) {
                cutLine += "-";
            }
            return cutLine;
        }
        else{
            return "-----";
        }
    }
}
